<?php
/**
 * episode controller
 * @package app-drama_controller
 */
load_controller('/base');
/**
 * episode controller
 * @author pandajingjing
 * @package app-drama_controller
 */
class episodecontroller extends basecontroller{

    function doRequest(){
        $this->setModule('episode');
        $arrErrInfo=array();
        $strURLAction=$this->getParam('action','url');
        $this->setData('strURLAction',$strURLAction);
        switch($strURLAction){
            case 'detail':
                return $this->doDetail();
                break;
            case 'import':
                return $this->doImport();
                break;
            case 'edit':
                return $this->doEdit();
                break;
            default:
                load_controller('/error/404');
                return new error_404controller();
                break;
        }
    }

    /**
	 * 获取访问该控制器的路径
	 */
    static function getURL($p_strAction='',$p_arrParam=array(),$p_bolSecure=false,$p_strPre=''){
        return parent::getURL('/episode',$p_strAction,$p_arrParam);
    }

    /**
	 * 显示节目详情
	 * @return error_404controller|string
	 */
    function doDetail(){
        $intSeriesID=$this->getParam('sid','url','int');
        if(null===$intSeriesID){
            load_controller('/error/404');
            return new error_404controller();
        }
        $strPostAction=$this->getParam('hidAction','post');
        load_lib('/bll/series');
        $clsBll=new bll_series();
        $arrSeries=$clsBll->getSeriesDetail($intSeriesID);
        if(null===$arrSeries){
            load_controller('/error/404');
            return new error_404controller();
        }
        if('send'==$strPostAction){
            load_lib('/bll/episode');
            $clsBllEp=new bll_episode();
            $intTerminalID=$this->getParam('dropList','post');
            $strFileKey=$this->getParam('hidFileKey','post');
            $clsBllEp->sendEpTerminal($intSeriesID,$strFileKey,$intTerminalID);
            load_controller('/queue');
            $this->redirectURL(queuecontroller::getURL());
        }else{
            $this->setData('arrSeries',$arrSeries);
            load_controller('/series');
            $this->setData('strEditSeriesURL',seriescontroller::getURL('edit',array( 
                    'sid'=>$intSeriesID 
            )));
            $this->setData('strAddEpURL',self::getURL('import',array( 
                    'sid'=>$intSeriesID 
            )));
            $this->setData('strEditEpURL',self::getURL('edit',array( 
                    'sid'=>$intSeriesID 
            )));
            load_lib('/bll/episode');
            $clsBllEp=new bll_episode();
            $arrEpList=$clsBllEp->searchEpisode(array( 
                    'SERIESID'=>$intSeriesID,
                    'DSTATUS'=>1 
            ));
            $this->setData('arrEpList',$this->filterEp($arrEpList));
        }
        return '/episode/detail';
    }

    /**
     * 生成输出所需要数据
     * @param array $p_arrEpList
     * @return array
     */
    function filterEp($p_arrEpList){
        $arrData=array();
        foreach($p_arrEpList as $arrEp){
            $strUniqKey=$arrEp['RATIOID'].'_'.$arrEp['DUBLANGID'];
            if(isset($arrData[$strUniqKey])){
                $intTmp=-1;
                foreach($arrData[$strUniqKey] as $intIndex=>$arrSubEp){
                    if(intval($arrSubEp['DORDER'])>intval($arrEp['DORDER'])){
                        $intTmp=$intIndex;
                        break;
                    }
                }
                if($intTmp>-1){
                    $arrData[$strUniqKey]=array_merge(array_slice($arrData[$strUniqKey],0,$intTmp),array( 
                            $arrEp 
                    ),array_slice($arrData[$strUniqKey],$intTmp));
                }else{
                    $arrData[$strUniqKey][]=$arrEp;
                }
            }else{
                $arrData[$strUniqKey]=array( 
                        $arrEp 
                );
            }
        }
        return $arrData;
    }

    /**
     * 添加剧集
     * @return error_404controller|string
     */
    function doImport(){
        $intSeriesID=$this->getParam('sid','url','int');
        if(null===$intSeriesID){
            load_controller('/error/404');
            return new error_404controller();
        }
        load_lib('/bll/series');
        $clsBll=new bll_series();
        $arrSeries=$clsBll->getSeriesDetail($intSeriesID);
        if(null===$arrSeries){
            load_controller('/error/404');
            return new error_404controller();
        }
        $this->setData('arrSeries',$arrSeries);
        load_controller('/series');
        $this->setData('strEditSeriesURL',seriescontroller::getURL('edit',array( 
                'sid'=>$intSeriesID 
        )));
        $this->setData('strAddEpURL',self::getURL('import',array( 
                'sid'=>$intSeriesID 
        )));
        $this->setData('strEditEpURL',self::getURL('edit',array( 
                'sid'=>$intSeriesID 
        )));
        $strPostAction=$this->getParam('hidAction','post');
        $arrErrInfo=array();
        load_lib('/bll/episode');
        $clsBllEp=new bll_episode();
        if('saveadd'==$strPostAction){
            $arrImportList=$this->getMulti();
            $arrImportErr=array();
            foreach($arrImportList as $arrImport){
                $mixResult=$clsBllEp->addEp($arrImport['FILEKEY'],$arrImport['HOSTID'],$arrImport['SIZE'],$arrImport['EXT'],$arrImport['FILENAME'],$intSeriesID,$arrImport['RATIOID'],$arrImport['LANGID'],$arrImport['ORDER']);
                if(is_array($mixResult)){
                    $arrImportErr[$arrImport['FILEKEY']]=$mixResult;
                }
            }
            $this->setData('arrImportErr',$arrImportErr);
            if(empty($arrImportErr)){
                $this->redirectURL(self::getURL('detail',array( 
                        'sid'=>$intSeriesID 
                )));
            }else{
                $arrUnImportList=$clsBllEp->getUnImportList();
                $this->setData('arrFileList',$arrUnImportList);
                $arrLangList=$clsBllEp->getLangList();
                $this->setData('arrLangList',$arrLangList);
                $arrRatioList=$clsBll->getRatioList();
                $this->setData('arrRatioList',$arrRatioList);
            }
        }else{
            $arrUnImportList=$clsBllEp->getUnImportList();
            $this->setData('arrFileList',$arrUnImportList);
            $arrLangList=$clsBllEp->getLangList();
            $this->setData('arrLangList',$arrLangList);
            $arrRatioList=$clsBll->getRatioList();
            $this->setData('arrRatioList',$arrRatioList);
            foreach($arrUnImportList as $intIndex=>$arrImport){
                $this->setViewState('frmEditEp','chkSelect_'.$arrImport['FILEKEY'],$arrImport['FILEKEY']);
                $this->setViewState('frmEditEp','dropRatio_'.$arrImport['FILEKEY'],0);
                $this->setViewState('frmEditEp','hidHostID_'.$arrImport['FILEKEY'],$arrImport['HOSTID']);
                $this->setViewState('frmEditEp','hidSize_'.$arrImport['FILEKEY'],$arrImport['FILESIZE']);
                $this->setViewState('frmEditEp','hidExt_'.$arrImport['FILEKEY'],$arrImport['EXT']);
                $this->setViewState('frmEditEp','hidFileName_'.$arrImport['FILEKEY'],$arrImport['FILENAME']);
                $this->setViewState('frmEditEp','dropLang_'.$arrImport['FILEKEY'],0);
                $this->setViewState('frmEditEp','txtOrder_'.$arrImport['FILEKEY'],$intIndex+1);
            }
            $this->setViewState('frmEditEp','hidAction','saveadd');
        }
        $this->setData('arrErrInfo',$arrErrInfo);
        return '/episode/edit';
    }

    /**
     * 获取所有添加记录
     * @return array
     */
    function getMulti(){
        $arrSelKeyList=$this->getParam('chkSelect','post');
        $arrRatioList=$this->getParam('dropRatio','post');
        $arrLangList=$this->getParam('dropLang','post');
        $arrOrderList=$this->getParam('txtOrder','post');
        $arrSizeList=$this->getParam('hidSize','post');
        $arrHostList=$this->getParam('hidHostID','post');
        $arrExtList=$this->getParam('hidExt','post');
        $arrFileNameList=$this->getParam('hidFileName','post');
        $arrMulti=array();
        if(is_array($arrSelKeyList)){
            foreach($arrSelKeyList as $strKey){
                $arrMulti[]=array( 
                        'FILEKEY'=>$strKey,
                        'RATIOID'=>$arrRatioList[$strKey],
                        'LANGID'=>$arrLangList[$strKey],
                        'ORDER'=>$arrOrderList[$strKey],
                        'SIZE'=>$arrSizeList[$strKey],
                        'HOSTID'=>$arrHostList[$strKey],
                        'EXT'=>$arrExtList[$strKey],
                        'FILENAME'=>$arrFileNameList[$strKey] 
                );
            }
        }
        return $arrMulti;
    }

    /**
     * 编辑操作
     * @return error_404controller|string
     */
    function doEdit(){
        $intSeriesID=$this->getParam('sid','url','int');
        if(null===$intSeriesID){
            load_controller('/error/404');
            return new error_404controller();
        }
        load_lib('/bll/series');
        $clsBll=new bll_series();
        $arrSeries=$clsBll->getSeriesDetail($intSeriesID);
        if(null===$arrSeries){
            load_controller('/error/404');
            return new error_404controller();
        }
        $this->setData('arrSeries',$arrSeries);
        load_controller('/series');
        $this->setData('strEditSeriesURL',seriescontroller::getURL('edit',array( 
                'sid'=>$intSeriesID 
        )));
        $this->setData('strAddEpURL',self::getURL('import',array( 
                'sid'=>$intSeriesID 
        )));
        $this->setData('strEditEpURL',self::getURL('edit',array( 
                'sid'=>$intSeriesID 
        )));
        $strPostAction=$this->getParam('hidAction','post');
        $arrErrInfo=array();
        load_lib('/bll/episode');
        $clsBllEp=new bll_episode();
        if('saveedit'==$strPostAction){
            $arrFileList=$this->getMulti();
            $arrUpdtErr=array();
            foreach($arrFileList as $arrFile){
                $mixResult=$clsBllEp->editEp($arrFile['FILEKEY'],$arrFile['RATIOID'],$arrFile['LANGID'],$arrFile['ORDER']);
                if(is_array($mixResult)){
                    $arrUpdErr[$arrFile['FILEKEY']]=$mixResult;
                }
                $this->setData('arrImportErr',$arrUpdErr);
            }
            if(empty($arrUpdErr)){
                $this->redirectURL(self::getURL('detail',array( 
                        'sid'=>$intSeriesID 
                )));
            }else{
                $arrEpList=$clsBllEp->searchEpisode(array( 
                        'SERIESID'=>$intSeriesID,
                        'DSTATUS'=>1 
                ));
                $this->setData('arrFileList',$arrEpList);
                $arrLangList=$clsBllEp->getLangList();
                $this->setData('arrLangList',$arrLangList);
                $arrRatioList=$clsBll->getRatioList();
                $this->setData('arrRatioList',$arrRatioList);
            }
        }else{
            $arrEpList=$clsBllEp->searchEpisode(array( 
                    'SERIESID'=>$intSeriesID,
                    'DSTATUS'=>1 
            ));
            $this->setData('arrFileList',$arrEpList);
            $arrLangList=$clsBllEp->getLangList();
            $this->setData('arrLangList',$arrLangList);
            $arrRatioList=$clsBll->getRatioList();
            $this->setData('arrRatioList',$arrRatioList);
            foreach($arrEpList as $intIndex=>$arrEp){
                $this->setViewState('frmEditEp','chkSelect_'.$arrEp['FILEKEY'],$arrEp['FILEKEY']);
                $this->setViewState('frmEditEp','dropRatio_'.$arrEp['FILEKEY'],$arrEp['RATIOID']);
                $this->setViewState('frmEditEp','hidHostID_'.$arrEp['FILEKEY'],$arrEp['HOSTID']);
                $this->setViewState('frmEditEp','hidSize_'.$arrEp['FILEKEY'],$arrEp['FILESIZE']);
                $this->setViewState('frmEditEp','hidExt_'.$arrEp['FILEKEY'],$arrEp['EXT']);
                $this->setViewState('frmEditEp','hidFileName_'.$arrEp['FILEKEY'],$arrEp['FILENAME']);
                $this->setViewState('frmEditEp','dropLang_'.$arrEp['FILEKEY'],$arrEp['DUBLANGID']);
                $this->setViewState('frmEditEp','txtOrder_'.$arrEp['FILEKEY'],$arrEp['DORDER']);
            }
            $this->setViewState('frmEditEp','hidAction','saveedit');
        }
        $this->setData('arrErrInfo',$arrErrInfo);
        return '/episode/edit';
    }
}